package model

import (
	"common/global"
	"gorm.io/gorm"
	"time"
)

type BargainActivity struct {
	Id            int64     `gorm:"column:id;type:mediumint;comment:活动ID;primaryKey;not null;" json:"id"`                                    // 活动ID
	ProductId     int64     `gorm:"column:product_id;type:mediumint;comment:关联的医疗商品ID;not null;" json:"product_id"`                          // 关联的医疗商品ID
	StartTime     time.Time `gorm:"column:start_time;type:datetime;comment:活动开始时间;not null;" json:"start_time"`                              // 活动开始时间
	EndTime       time.Time `gorm:"column:end_time;type:datetime;comment:活动结束时间;not null;" json:"end_time"`                                  // 活动结束时间
	OriginalPrice float32   `gorm:"column:original_price;type:decimal(8, 2);comment:商品原价;not null;" json:"original_price"`                   // 商品原价
	TargetPrice   float32   `gorm:"column:target_price;type:decimal(8, 2);comment:砍价目标价格;not null;" json:"target_price"`                     // 砍价目标价格
	MinPrice      float32   `gorm:"column:min_price;type:decimal(8, 2);comment:最低可砍至价格;not null;" json:"min_price"`                          // 最低可砍至价格
	TotalCuts     int64     `gorm:"column:total_cuts;type:int;comment:总需砍价次数;not null;default:10;" json:"total_cuts"`                        // 总需砍价次数
	MinCutAmount  float32   `gorm:"column:min_cut_amount;type:decimal(8, 2);comment:单次最小砍价金额;not null;default:0.01;" json:"min_cut_amount"`  // 单次最小砍价金额
	MaxCutAmount  float32   `gorm:"column:max_cut_amount;type:decimal(8, 2);comment:单次最大砍价金额;not null;default:99.99;" json:"max_cut_amount"` // 单次最大砍价金额
	Stock         int64     `gorm:"column:stock;type:int;comment:砍价专用库存;not null;default:0;" json:"stock"`                                   // 砍价专用库存
	CreatedAt     time.Time `gorm:"column:created_at;type:datetime;comment:创建时间;not null;" json:"created_at"`                                // 创建时间
	UpdatedAt     time.Time `gorm:"column:updated_at;type:datetime;comment:更新时间;not null;" json:"updated_at"`                                // 更新时间
}

func (m *BargainActivity) TableName() string {
	return "bargain_activity"
}

func GteBargainActivity(activityId, productId int64) (result *BargainActivity, err error) {
	err = global.Db.Model(&BargainActivity{}).Where("id = ? AND product_id = ?", activityId, productId).Limit(1).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func DeductBargainProductStock(activityId int64) error {
	err := global.Db.Model(&BargainActivity{}).Where("id =? AND stock > 0", activityId).UpdateColumn("stock", gorm.Expr("stock - 1")).Error
	return err
}

func BargainProductList(offset int64, size int64) (result []*BargainActivity, err error) {
	err = global.Db.Model(&BargainActivity{}).Where("stock > 0").Limit(int(size)).Offset(int(offset)).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func GetBargainProductInfo(id int64) (result *BargainActivity, err error) {
	err = global.Db.Model(&BargainActivity{}).Where("id =?", id).Limit(1).Find(&result).Error
	return
}
